/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.sys.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.sys.dao.StoreDao;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 门店Controller
 * @author lvbo
 * @version 2013-06-21
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/sys/setting")
public class SettingController extends BaseController {
	@Autowired
	private StoreDao storeDao;
	
	@RequestMapping(value = "loginpw")
	public String loginpw(Model model) {
		Store store = StoreUtils.getStore();
		model.addAttribute("status", store.getNeed_login_pw());
		return "modules/sys/loginpw";
	}

	@RequestMapping(value = "loginPwForm")
	public String loginPwForm(Model model) {
		return "modules/sys/loginPwForm";
	}
	
	@RequestMapping(value = "bossPwForm")
	public String bossPwForm(Model model) {
		return "modules/sys/bossPwForm";
	}
	
	@RequestMapping(value = "changeBossPW")
	public String changeBossPW(String oldPw, String newPw1, String newPw2,
			Model model, RedirectAttributes redirectAttributes) {
		Store store = StoreUtils.getStore();
		if(!store.getBossPassword().equals(oldPw)) {
			addMessage(redirectAttributes, "管理密码输入错误！");
		} else {
			store.setBossPassword(newPw1);
			storeDao.save(store);
			addMessage(redirectAttributes, "管理密码更改成功！");
		}
		return "redirect:"+Global.ADMIN_PATH+"/sys/setting/bossPwForm/";
	}
	
	@RequestMapping(value = "changeLoginPW")
	public String changeLoginPW(String oldPw, String newPw1, String newPw2,
			Model model, RedirectAttributes redirectAttributes) {
		Store store = StoreUtils.getStore();
		if(!store.getLoginPassword().equals(oldPw)) {
			addMessage(redirectAttributes, "任务密码输入错误！");
		} else {
			store.setLoginPassword(newPw1);
			storeDao.save(store);
			addMessage(redirectAttributes, "任务密码更改成功！");
		}
		return "redirect:"+Global.ADMIN_PATH+"/sys/setting/loginPwForm";
	}
	
	@RequestMapping(value = "setting")
	public String setting(HttpServletRequest request, RedirectAttributes redirectAttributes) {
		String need = request.getParameter("status");
		Store store = StoreUtils.getStore(true);
		int isNeed = 0;
		if (need != null) {
			 try {
				isNeed = Integer.parseInt(need);
			} catch (NumberFormatException e) {
			}
		}
		store.setNeed_login_pw(isNeed);
		storeDao.save(store);
		addMessage(redirectAttributes, "设置成功！");
		return "redirect:"+Global.ADMIN_PATH+"/sys/setting/loginpw";
	}
}
